package com.global.model;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;

/**
 * This class is used to represent available roles in the database.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 *         Version by Dan Kibler dan@getrolling.com
 *         Extended to implement Acegi GrantedAuthority interface
 *         by David Carter david@carter.net
 */
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate=true)
@Table(name = "ZLY_SE_ROLE")
@NamedQueries({
        @NamedQuery(
                name = "findRoleByName",
                query = "select r from Role r where r.name = :name "
        )
})
@SequenceGenerator(name="ZLY_SE_ROLE_ID_GENERATOR", sequenceName="ZLY_SE_ROLE_ID_SEQ", initialValue=1, allocationSize=1)
public class Role extends BaseObject implements Serializable, GrantedAuthority {

	private static final long serialVersionUID = 5296231780364386125L;
	
	private Long id;
    private String name;
    private String description;
	
    /**
     * 定义操作人、操作时间
     */
    private Who who;
    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Role() {
    }

    public Role(String name, String description) {
    	this.name = name;
    	this.description = description;
    }
    
    /**
     * Create a new instance and set the name.
     *
     * @param name name of the role.
     */
    public Role(final String name) {
        this.name = name;
    }

	/**
     * {@inheritDoc}
     */
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ZLY_SE_ROLE_ID_GENERATOR")
    public Long getId() {
        return id;
    }
	/**
     * {@inheritDoc}
     */
    public void setId(Long id) {
        this.id = id;
    }
    
    /**
     * @return the name property (getAuthority required by Acegi's GrantedAuthority interface)
     * @see org.springframework.security.core.GrantedAuthority#getAuthority()
     */
    @Transient
    public String getAuthority() {
        return getName();
    }
    
    @Column(unique=true, length = 128)
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @Column(length = 1024)
    public String getDescription() {
        return this.description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    
    /**
     * {@inheritDoc}
     */
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		
		final Role other = (Role) obj;
		
		if (id != other.id){
			if (name == null) {
				if (other.name != null){
					return false;
				}
			} else if (!name.equalsIgnoreCase(other.name)){
				return false;
			}
		}
		
		return true;
	}
    
    /**
     * {@inheritDoc}
     */
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
    
    /**
     * {@inheritDoc}
     */
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
                .append(this.name)
                .toString();
    }

	/**
     * {@inheritDoc}
     */
    public int compareTo(Object o) {
        return (equals(o) ? 0 : -1);
    }

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Who getWho() {
		return who;
	}
	/**
     * {@inheritDoc}
     */
	@Override
	public void setWho(Who who) {
		this.who = who;
	}
}
